<template>
    <div>
        <basic-container>
            <div class="app-container">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
                    <el-form-item label="订单编号">
                        <el-input v-model="queryParams.orders" placeholder="请输入设备名称" clearable @clear="getList" />
                    </el-form-item>

                    <el-form-item label="工单完成状态">
                        <el-select v-model="queryParams.status" placeholder="请选择" clearable @clear="getList">
                            <el-option v-for="item in dataMuen" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                        <!-- <el-input v-model="queryParams.status" placeholder="请输入设备编号"  /> -->
                    </el-form-item>


                    <el-form-item label="日期查询">
                        <el-date-picker v-model="value" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                            @click="handleAdd">新增工单</el-button>
                    </el-col>
                </el-row>
                <el-table v-loading="loading" :data="menuList" row-key="user_id" :default-expand-all="isExpandAll"
                    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
                    <el-table-column prop="aircraft" label="机种"></el-table-column>
                    <el-table-column prop="name" label="工单名称" align="center"></el-table-column>
                    <el-table-column prop="orders" label="订单编号" align="center"></el-table-column>
                    <el-table-column prop="amount" label="计划生产数量" align="center"></el-table-column>
                    <el-table-column prop="time" label="计划生产时间" align="center"></el-table-column>
                    <el-table-column prop="delivery" label="生产完工时间" align="center"></el-table-column>
                    <el-table-column prop="totals" label="总生产量" align="center"></el-table-column>
                    <el-table-column prop="actual" label="合格数量" align="center"></el-table-column>
                
                    <!-- <el-table-column prop="sample" label="是否样件" align="center">
                        <template slot-scope="scope">{{ scope.row.sample == 1 ? '是' : '否' }}</template>
</el-table-column> -->
                    <el-table-column prop="status" label="工单状态" align="center"></el-table-column>
                    <el-table-column prop="" label="合格率" align="center">

                        <template slot-scope="scope">
                            {{ scope.row.actual == 0 ? null : ((scope.row.actual / scope.row.totals) * 100).toFixed(2) +
                    '%' }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status2" label="换型" align="center">

                        <template slot-scope="scope"><el-switch v-model="scope.row.status2"
                                @change="handleBlur(scope.row, scope.$index)">
                            </el-switch></template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit"
                                @click="handleUpdate(scope.row)">修改</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete"
                                @click="handleDelete(scope.row, 1)">
                                删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.index"
                    :limit.sync="queryParams.size" @pagination="getList" />
                <!-- 添加或修改菜单对话框 -->
                <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body
                    :close-on-click-modal="false">
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                        <el-row>
                            <el-col :span="19">
                                <el-form-item label="订单编号" prop="orders">
                                    <el-input v-model="form.orders" placeholder="请输入设备工站" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="任务名称" prop="name">
                                    <el-input v-model="form.name" placeholder="请输入工站名称" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="机种名称" prop="aircraft_id">
                                    <el-select v-model="form.aircraft_id" placeholder="请选择" style="width: 100%;">
                                        <el-option v-for="item in options" :key="item.key" :label="item.value"
                                            :value="item.key">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="计划生产数量" prop="amount">
                                    <el-input v-model="form.amount" placeholder="请输入工站编号" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="计划生产时间" prop="time">
                                    <el-date-picker v-model="form.time" value-format="yyyy-MM-dd" type="date"
                                        placeholder="选择日期" style="width: 100%;">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <!-- <el-col :span="19">
                                <el-form-item label="是否样件工单" prop="sample">
                                    <el-select v-model="form.sample" placeholder="请选择" style="width: 100%;">
                                        <el-option v-for="item in optionsNum" :key="item.value" :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col> -->
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="submitForm">确 定</el-button>
                        <el-button @click="cancel">取 消</el-button>
                    </div>
                </el-dialog>
            </div>
        </basic-container>
    </div>
</template>

<script>
import {
    addtasks,
    gettaskslist,
    grtAircraft,
    eidTasks,
    dleTasks,
    eidStatus
} from "@/api/tasks";


export default {
    data() {
        return {
            // 遮罩层
            loading: false,
            showSearch: true,
            value: [],
            total: 1,
            inMess: [],
            menuList: [],
            roleOptions: [],
            dataMuen: [
                {
                    value: 'unfinished',
                    label: "未完成",
                },
                {
                    value: "finished",
                    label: "已完成",
                },
                {
                    value: "running",
                    label: "进行中",
                },
            ],
            optionsNum: [
                {
                    value: 0,
                    label: "否",
                },
                {
                    value: 1,
                    label: "是",
                },
            ],
            options: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部折叠
            isExpandAll: false,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                index: 1,
                size: 10,
                orders: null,
                status: null,
                start: null,
                finish: null,
            },
            // 表单参数
            form: {
                orders: null,
                name: null,
                amount: null,
                time: null,
                aircraft_id: null,
                status: null,
                sample: 0,
            },
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "任务名称不能为空", trigger: "blur" },
                ],
                aircraft_id: [
                    { required: true, message: "设备编号不能为空", trigger: "blur" },
                ],
                station: [
                    { required: true, message: "设备工站不能为空", trigger: "blur" },
                ],
                workplace: [
                    { required: true, message: "设备地址不能为空", trigger: "blur" },
                ],
                time: [
                    { required: true, message: "设备添购时间不能为空", trigger: "blur" },
                ],
                expired: [
                    { required: true, message: "设备有效日期不能为空", trigger: "blur" },
                ],
            },
        };
    },
    created() {
        this.getList();
        this.gitAicftList()
        // this.startPolling();
    },
    computed: {

    },
    beforeDestroy() {
        // this.stopPolling();
    },
    methods: {
        // startPolling() {
        //     this.pollingTimer = setInterval(this.getList, 10000); // 每10秒刷新一次（根据需要进行调整）
        // },
        // stopPolling() {
        //     clearInterval(this.pollingTimer);
        // },
        // 匹配电话号码
        handleBlur(row) {

            let queData = {
                task_id: row.task_id,
                status: row.status2 ? 1 : 0,
            }
            this.$confirm('此操作将切换当前机种，是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                eidStatus(queData).then(res => {
                    console.log(res);
                    this.$message({
                        type: 'success',
                        message: '换型成功!'
                    });
                    this.getList();
                })
                this.getList();
            }).catch((err) => {
                console.log(err);
                this.getList();
            });

        },
        onPhoneInput(event) {
            let value = event;
            // 使用正则表达式过滤非数字字符
            value = value.replace(/\D/g, "");
            // 更新输入框的值
            this.form.phone = value;
        },
        /** 查询菜单列表 */
        getList() {
            this.loading = true;
            this.queryParams.status = this.queryParams.status == '' ? null : this.queryParams.status
            this.queryParams.orders = this.queryParams.orders == '' ? null : this.queryParams.orders
            gettaskslist(this.queryParams).then((response) => {
                this.menuList = response.data.data;
                this.total = response.data.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.getList();
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                orders: null,
                name: null,
                amount: null,
                time: null,
                aircraft_id: null,
                status: null,
                sample: 0,
            };
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.start = this.value == null ? null : this.value.length > 0 ? this.value[0] : null
            this.queryParams.finish = this.value == null ? null : this.value.length > 0 ? this.value[1] : null

            this.getList();

        },
        /** 重置按钮操作 */
        resetQuery() {
            this.value = null
            this.queryParams = {
                orders: null,
                index: 1,
                size: 10,
                status: null,
                start: null,
                finish: null
            };
            this.getList();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.open = true;
            this.reset();
            this.title = "新增工单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {

            this.reset();
            this.form = row;
            this.open = true;
            this.title = "修改工单";
        },
        //机种序列// let arrList = res.data.data
        // grtAircraft()
        gitAicftList() {
            grtAircraft().then(res => {
                this.options = res.data.data
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.title == "新增工单") {
                        addtasks(this.form).then(() => {
                            this.$message({
                                message: "添加成功",
                                type: "success",
                            });
                            this.open = false;
                            this.getList();
                            this.reset();
                        });
                    } else {
                        eidTasks(this.form).then(() => {
                            this.$message({
                                message: "修改成功",
                                type: "success",
                            });
                            this.open = false;
                            this.getList();
                            this.reset();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            let messsId = {
                task_id: row.task_id
            }
            this.$confirm('是否确认删除名称为"' + row.name + '"的工单？')
                .then(() => {
                    dleTasks(messsId).then(() => {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                        });
                        this.open = false;
                        this.getList();
                        this.reset();
                    });
                })
                .catch(() => { });
        },
    },
};
</script>
